//
//  RecordTableViewCell.swift
//  IDF
//
//  Created by 陈徐挺 on 27/03/2018.
//  Copyright © 2018 Jesse.chen. All rights reserved.
//

import UIKit
import Reusable
import YYCategories



class RecordTableViewCell: UITableViewCell, NibReusable {

    @IBOutlet weak var label_1: UILabel!
    @IBOutlet weak var label_2: UILabel!
    @IBOutlet weak var label_3: UILabel!
    @IBOutlet weak var label_4: UILabel!
    @IBOutlet weak var label_5: UILabel!
    @IBOutlet weak var label_6: UILabel!
    @IBOutlet weak var label_8: UILabel!
    @IBOutlet weak var label_8_y: NSLayoutConstraint!
    @IBOutlet weak var repaymentBtn: UIButton!
    @IBOutlet weak var label_0: UILabel!
    @IBOutlet weak var label_7: UILabel!
    @IBOutlet weak var extendBtn: UIButton!
    
    @IBOutlet weak var label_8_height: NSLayoutConstraint!
    var record: Record!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        label_8_y.constant = 80
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setCell(_ model: Record) {
        let status = model.status
        record = model
        label_4.isHidden = true
        label_5.isHidden = true
        label_6.isHidden = true
        label_7.isHidden = true
        label_8.isHidden = true
        repaymentBtn.isHidden = true
        extendBtn.isHidden = true
        label_8_y.constant = 80
        label_3.textColor = UIColor(hexString: "333333")
        label_8.layer.borderColor = UIColor(hexString: "e5e5e5")?.cgColor
        label_8.backgroundColor = UIColor(hexString: "fbfbfb")
        switch status {
        case -2:
            label_3.textColor = UIColor(hexString: "e02e44")
            label_3.text = "Repayment failure"
            label_4.isHidden = false
            label_4.text = "Request date: \(model.createTime!)"
            label_5.isHidden = false
            label_5.text = "Release loan date: \(model.releaseLoanTime!)"
            label_6.isHidden = false
            label_6.text = "Payback date: \(model.repaymentTime!)"
            label_7.isHidden = false
            label_7.text = "Packback money: Rp \(model.payBackMoney! + model.overdueDay! * Int(model.overdueInterset!))"
            label_8.layer.borderColor = UIColor(hexString: "fad4d4")?.cgColor
            label_8.backgroundColor = UIColor(hexString: "fffafa")
            repaymentBtn.isHidden = false
            extendBtn.isHidden = false
        case -1:
            label_3.textColor = UIColor(hexString: "e02e44")
            label_3.text = "Audit failure"
            label_4.isHidden = false
            label_4.text = "Request date: \(model.createTime!)"
            label_5.isHidden = false
            label_5.text = "Audit date: \(model.auditTime!)"
            label_8.layer.borderColor = UIColor(hexString: "fad4d4")?.cgColor
            label_8.backgroundColor = UIColor(hexString: "fffafa")
        case 0:
            label_3.textColor = UIColor(hexString: "ffa91b")
            label_3.text = "Auditing"
            label_4.isHidden = false
            label_4.text = "Request date: \(model.createTime!)"
            label_8.layer.borderColor = UIColor(hexString: "ffa91b")?.cgColor
        case 1:
            label_3.textColor = UIColor(hexString: "60b800")
            label_3.text = "Through audit"
            label_4.isHidden = false
            label_4.text = "Request date: \(model.createTime!)"
            label_5.isHidden = false
            label_5.text = "Release loan date: \(model.releaseLoanTime!)"
            label_6.isHidden = false
            label_6.text = "Payback date: \(model.repaymentTime!)"
            label_7.isHidden = false
            label_7.text = "Packback money: Rp \(model.payBackMoney! + model.overdueDay! * Int(model.overdueInterset!))"
            label_8.layer.borderColor = UIColor(hexString: "60b800")?.cgColor
            repaymentBtn.isHidden = false
            extendBtn.isHidden = false
            break
        case 2:
            label_3.textColor = UIColor(hexString: "ffa91b")
            label_3.text = "Repaid audit"
            label_4.isHidden = false
            label_4.text = "Request date: \(model.createTime!)"
            label_5.isHidden = false
            label_5.text = "Release loan date: \(model.releaseLoanTime!)"
            label_6.isHidden = false
            label_6.text = "Payback date: \(model.repaymentTime!)"
            label_7.isHidden = false
            label_7.text = "Packback money: Rp \(model.payBackMoney! + model.overdueDay! * Int(model.overdueInterset!))"
            label_8.layer.borderColor = UIColor(hexString: "ffa91b")?.cgColor
            break
        case 3:
            label_3.textColor = UIColor(hexString: "60b800")
            label_3.text = "Repaid"
            label_4.isHidden = false
            label_4.text = "Payback date: \(model.repaymentTime!)"
            label_5.isHidden = false
            label_5.text = "Audit date: \(model.auditTime!)"
            label_8.layer.borderColor = UIColor(hexString: "60b800")?.cgColor
            break
        case 4:
            label_3.textColor = UIColor(hexString: "e02e44")
            label_3.text = "Overdue"
            label_4.isHidden = false
            label_4.text = "Request date: \(model.createTime!)"
            label_5.isHidden = false
            label_5.text = "Release loan date: \(model.releaseLoanTime!)"
            label_6.isHidden = false
            label_6.text = "Payback date: \(model.repaymentTime!)"
            label_7.isHidden = false
            label_7.text = "Packback money: Rp \(model.payBackMoney! + model.overdueDay! * Int(model.overdueInterset!))"
            label_8.layer.borderColor = UIColor(hexString: "e02e44")?.cgColor
            repaymentBtn.isHidden = false
            extendBtn.isHidden = false
            break
        case 5:
            label_3.textColor = UIColor(hexString: "ffa91b")
            label_3.text = "Auditing"
            label_4.isHidden = false
            label_4.text = "Request date: \(model.createTime!)"
            label_5.isHidden = false
            label_5.text = "Audit date: \(model.auditTime!)"
            label_8.layer.borderColor = UIColor(hexString: "ffa91b")?.cgColor
            break
        case 6:
            label_3.textColor = UIColor(hexString: "ffa91b")
            label_3.text = "Audit Extension"
            label_4.isHidden = false
            label_4.text = "Request date: \(model.createTime!)"
            label_8.layer.borderColor = UIColor(hexString: "ffa91b")?.cgColor
        default:
            break
        }
        label_0.text = "NO. \(record.debitId!)"
        label_1.text = "Rp\(record.debitMoney!)"
        label_2.text = "\(record.debitPeroid!) Days"
        
        if record.auditInfo! != "" {
            let string = NSString(string: record.auditInfo!)
            let height = string.height(for: UIFont.systemFont(ofSize: 12), width: kWidth - 30)
            if height > 40 {
                label_8_height.constant = height + 10
            } else {
                label_8_height.constant = 40
            }
            label_8.text = record.auditInfo!
            label_8.isHidden = false
        }
    }
    
    
    func getCellHeight() -> CGFloat {
        var height: CGFloat = 68
        if !label_5.isHidden {
            height = height + 10 + 15
            label_8_y.constant = 100
        }
        if !label_6.isHidden {
            height = height + 10 + 15
            label_8_y.constant = 123
        }
        if !label_7.isHidden {
            height = height + 10 + 15
            label_8_y.constant = 150
        }
        if !label_8.isHidden {
            height = height  + label_8_height.constant + 15
        }
        if !repaymentBtn.isHidden {
            height = height + 40
        }
        height = height + 20
        return height
    }
    

    @IBAction func extend(_ sender: UIButton) {
        let next: RepaymentViewController = initViewContollerWithStoryboard(storyboardName: "Record", storyboardId: "repaymentViewController")
        next.record = record
        next.isExtend = true
        nowNavgationController.pushViewController(next, animated: true)
    }
    
    @IBAction func repayment(_ sender: UIButton) {
        let next: RepaymentViewController = initViewContollerWithStoryboard(storyboardName: "Record", storyboardId: "repaymentViewController")
        next.record = record
        nowNavgationController.pushViewController(next, animated: true)
        
        
    }
    
    
}
